package com.jeegod.core.system.controller;

import com.jeegod.core.base.BaseController;
import com.jeegod.core.base.Result;
import com.jeegod.core.constants.BaseResultEnums;
import com.jeegod.core.system.dto.User;
import com.jeegod.core.system.service.UserService;
import com.jeegod.core.util.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@RequestMapping("/sys/user")
@RestController
public class UserController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @PostMapping("/queryAll")
    public Result queryAll(){
        List<User> list = userService.selectAll();
        return Results.successWithData(list, BaseResultEnums.SUCCESS.code(), BaseResultEnums.SUCCESS.desc());
    }

    @RequestMapping("/queryOne/{userId}")
    public Result queryOne(@PathVariable Long userId){
        User user = userService.get(userId);
        return Results.successWithData(user);
    }

    @PostMapping("/save")
    public Result save(@Valid @RequestBody User user){
        user = userService.insertSelective(user);
        return Results.successWithData(user);
    }

    @PostMapping("/update")
    public Result update(@Valid @RequestBody List<User> user){
        for (User u : user) {
            logger.debug("userId:{},username:{},birthday:{}", u.getUserId(), u.getUsername(), u.getBirthday());
            logger.info("userId:{},username:{},birthday:{}", u.getUserId(), u.getUsername(), u.getBirthday());
            logger.error("userId:{},username:{},birthday:{}", u.getUserId(), u.getUsername(), u.getBirthday());
        }
        user = userService.persistSelective(user);
        return Results.successWithData(user);
    }

    @RequestMapping("/delete")
    public Result delete(User user){
        userService.delete(user);
        return Results.success();
    }

    @RequestMapping("/delete/{userId}")
    public Result delete(@PathVariable Long userId){
        userService.delete(userId);
        return Results.success();
    }

}
